package com.slz3.business.module.afterSaleManagement.aftApplyOrder.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.afterSaleManagement.aftApplyOrder.domain.AftApplyOrder;
import com.slz3.business.module.afterSaleManagement.aftApplyOrder.domain.AftApplyOrderFactory;
import com.slz3.business.module.afterSaleManagement.aftApplyOrder.steward.AftApplyOrderSpecification;
import com.slz3.business.module.orderManagement.ordRefund.steward.OrdRefundSynr;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AftApplyOrderServiceImpl implements AftApplyOrderService {
    @Override
    public AftApplyOrder save(AftApplyOrder aftApplyOrder) {
        return Repositorys.aftApplyOrder.save(AftApplyOrderFactory.initBaseFields(aftApplyOrder));
    }
    @Override
    public Page<AftApplyOrder> page(MultiValueMap<String, String> params) {
        Page<AftApplyOrder> aftApplyOrderPage = Repositorys.aftApplyOrder.findAll(
                AftApplyOrderSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return aftApplyOrderPage;
    }


    @Override
    public AftApplyOrder modify(AftApplyOrder aftApplyOrderNew) {
        return Repositorys.aftApplyOrder.saveAndFlush(AftApplyOrderFactory.initBaseFields(aftApplyOrderNew));
    }

    @Override
    public Optional<AftApplyOrder> single(UUID uuid) {
        return Repositorys.aftApplyOrder.findById(uuid);
    }

    @Override
    public List<AftApplyOrder> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.aftApplyOrder.findAllById(uuids);
    }

    @Override
    public List<AftApplyOrder> delete(List<AftApplyOrder> aftApplyOrderList) {
        aftApplyOrderList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            AftApplyOrderFactory.initBaseFields(item);
        });
        return Services.aftApplyOrderTransactional.batchModify(aftApplyOrderList);
    }

    @Override
    public void sync() {
        //get sync key
        String syncListKey = String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_APPLYAFTERSALELIST);
        String syncSetKey = String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_APPLYAFTERSALELIST.concat("Set"));

        List<String> afterSaleUuidList = Jedis.db0.getListStartEnd(syncListKey,0,-1);
        do{
            afterSaleUuidList.stream().filter(item->item!=null).forEach(item->{
                OrdRefundSynr.saveAfterSaleUtilMethod(item);
                Jedis.db0.removeFromList(syncListKey,0,item);
                Jedis.db0.removeFromSet(syncSetKey,item);
            });
            afterSaleUuidList = Jedis.db0.getSet(syncSetKey).stream().filter(item->item!=null).map(item->item.toString()).collect(Collectors.toList());
        }while (VE.isNotEmpty(afterSaleUuidList));

    }
}
